package com.mirror.utils.ui;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.MeasureSpec;

public class UiUtils {

	@SuppressWarnings("deprecation")
	public static int[] getDisplaySize(Context context) {
		int[] size = new int[2];
		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		size[0] = display.getWidth();
		size[1] = display.getHeight();
		return size;
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) Math.ceil(dipValue * scale);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) Math.ceil(pxValue / scale);
	}

	public static Bitmap convertViewToBitmap(View view) {
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();
		return bitmap;
	}

	public static Bitmap mosaicImage(List<Bitmap> bImageList, int col) {
		Bitmap bitmap = null;
		Canvas canvas = null;
		for (int i = 0; i < bImageList.size(); i++) {
			Bitmap b = bImageList.get(i);
			if (bitmap == null) {
				bitmap = Bitmap.createBitmap(b.getWidth() * col,
				                             b.getHeight() * (bImageList.size() % col == 0 ? bImageList.size() / col : bImageList.size() / col + 1),
				                             b.getConfig());
				canvas = new Canvas(bitmap);
			}
			canvas.drawBitmap(b, (i % col) * b.getWidth(), (i / col) * b.getHeight(), null);
		}
		return bitmap;
	}

}
